<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：User.class.php
 * @类功能: 用户控制器
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\credit\controller;

use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\agent\IAgentCache;
use app\common\cache\common\IAreaCache;
use app\common\cache\user\IUserCache;
use app\common\controller\ApiBaseController;
use app\common\controller\CreditBaseController;
use app\common\enum\AgentAdminStatus;
use app\common\enum\AppType;
use app\common\enum\AuditStatus;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsAccumulationFund;
use app\common\enum\IsAnonymous;
use app\common\enum\IsAuthentication;
use app\common\enum\IsCar;
use app\common\enum\IsCompany;
use app\common\enum\IsCreditCard;
use app\common\enum\IsDelete;
use app\common\enum\IsHouse;
use app\common\enum\IsLeader;
use app\common\enum\IsMarry;
use app\common\enum\IsPayPwd;
use app\common\enum\IsPhone;
use app\common\enum\AdminSuper;
use app\common\enum\IsPolicy;
use app\common\enum\IsReal;
use app\common\enum\IsSocial;
use app\common\enum\IsTrack;
use app\common\enum\IsYes;
use app\common\enum\PoolBidStatus;
use app\common\enum\Source;
use app\common\enum\TenderStatus;
use app\common\enum\UserStatus;
use app\common\ext\IBid;
use app\common\ext\IConfig;
use app\common\ext\IDb;
use app\common\ext\IFake;
use app\common\ext\IIdentity;
use app\common\ext\ILog;
use app\common\ext\ISms;
use app\common\ext\IJuHe;
use app\common\ext\IUrl;

class User extends CreditBaseController {

    /**
     * @功能：用户注册
     * @开发者：cxl
     */
    public function reg(){
        //接收参数
        $user_phone = self::get_data('phone',self::PHONE,"请输入正确的手机号码！");
        $user_pwd = self::get_data('pwd',self::NOT_EMPTY,"请输入密码！");
        $user_verification = self::get_data('verification',self::NOT_EMPTY,"请输入短信验证码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::XD,$user_phone);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("无效的验证码！");
            self::send();
        }

        //判断手机号是否已经存在
        $user_where['phone'] = $user_phone;
        $user_count = IDb::getInstance("agent_admin")->setDbWhere($user_where)->con();
        if($user_count > 0){
            self::set_code(self::WARNING_0002);
            self::set_msg("亲，您输入的手机号已经被注册了！");
            self::send();
        }

        //开始事物出来
        IDb::dbStartTrans();

        $agent_id = IDb::getInstance("agent")->setDbData(['is_delete'=>IsDelete::No])->add();
        if($agent_id === false){
            //回滚事物
            IDb::dbRollback();

            self::set_code(self::WARNING_0004);
            self::set_msg("注册失败，请联系客服！");
            self::send();
        }

        //添加用户信息
        $user_data['agent_id'] = $agent_id;
        $user_data['phone'] = $user_phone;
        $user_data['admin_super'] = AdminSuper::Super;
        $user_data['is_leader'] = IsLeader::Yes;
        $user_data['nick'] = get_string_rsa($user_phone, 3, 4) . "_" . rand(1000,9999);
        $user_data['pwd'] = md5(md5($user_pwd));
        $user_data['reg_ip'] = get_client_ip();
        $user_data['reg_time'] = get_date_time();
        $user_id = IDb::getInstance("agent_admin")->setDbData($user_data)->add();
        if($user_id === false){
            //回滚事物
            IDb::dbRollback();

            //返回错误
            self::set_code(self::WARNING_0003);
            self::set_msg("注册失败，请联系客服！");
            self::send();
        }

        //提交事物
        IDb::dbCommit();

        //判断手机号是否已经存在
        $user_info = IAgentAdminCache::getInfo($user_id);
        if(empty($user_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得登录信息失败！");
            self::send();
        }

        //设置身份证信息
        if(!empty($user_info['real'])){
            $user_info['real'] = get_string_rsa($user_info['real'],1);
        }

        //设置身份证信息
        if(!empty($user_info['identity'])){
            $user_info['identity'] = get_string_rsa($user_info['identity'],5,8);
        }

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        //清除缓存
        IAgentAdminCache::deleteList();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：用户登录
     * @开发者：cxl
     */
    public function login(){
        //接收参数
        $user_phone = self::get_data('phone',self::PHONE,"请输入正确的手机号码！");
        $user_pwd = self::get_data('pwd',self::NOT_EMPTY,"请输入密码！");

        //判断手机号是否已经存在
        $user_where['phone'] = $user_phone;
        $user_info = IDb::getInstance("agent_admin")->setDbWhere($user_where)->row();
        if(empty($user_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的手机号未注册！");
            self::send();
        }

        //判断用户是否被加入黑名单
        if($user_info['agent_admin_status'] == AgentAdminStatus::blacklist){
            self::set_code(self::ERR_CHECK_0006);
            self::set_msg("亲，您的账号已被禁用，请联系客服！");
            self::send();
        }

        //判断密码是否正确
        if(md5(md5($user_pwd)) != $user_info['pwd']){
            self::set_code(self::WARNING_0003);
            self::set_msg("亲，您输入的密码错误！");
            self::send();
        }else{
            //设置身份证信息
            if(!empty($user_info['real'])){
                $user_info['real'] = get_string_rsa($user_info['real'],1);
            }

            //设置身份证信息
            if(!empty($user_info['identity'])){
                $user_info['identity'] = get_string_rsa($user_info['identity'],5,8);
            }

            //用户密码信息
            unset($user_info['pwd'],$user_info['pay_pwd']);
        }

        //修改最后登录时间
        $user_data = null;
        $user_where = null;
        $user_where['agent_admin_id'] = $user_info['agent_admin_id'];
        $user_data['login_ip'] = get_client_ip();
        $user_data['login_time'] = get_date_time();
        $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0004);
            self::set_msg("登录失败！");
            self::send();
        }

        //判断是否是首次登录
        $first_login = IsYes::not;
        if(empty($user_info['login_time'])){
            $first_login = IsYes::yes;
        }

        //设置数据
        self::set_token('_u',$user_info['agent_admin_id']); //设置设备编号

        //清除用户缓存
        IAgentAdminCache::deleteInfo($user_info['agent_admin_id']);

        $agent_info = IAgentCache::getInfo($user_info['agent_id']);
        if(!$agent_info){
            $agent_info = [];
        }
        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,self::get_token());
        self::set_values($user_info);
        self::set_value('agent_info',$agent_info);
        self::set_value('first_login',$first_login);
        self::send();
    }

    /**
     * @功能：用户短信登录
     * @开发者：cxl
     */
    public function sms_login(){
        //接收参数
        $os = self::$oid;
        $user_phone = self::get_data('phone',self::PHONE,"请输入正确的手机号码！");
        $user_verification = self::get_data('verification',self::NOT_EMPTY,"请输入短信验证码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::XD,$user_phone);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的短信验证码错误！");
            self::send();
        }

        //判断手机号是否已经存在
        $user_where['phone'] = $user_phone;
        $user_info = IDb::getInstance("agent_admin")->setDbWhere($user_where)->row();
        if($user_info === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("短信登录失败，请联系客服！");
            self::send();
        }

        $first_login = IsYes::not;

        //判断用户是否存在
        if(empty($user_info)){
            //开始事物出来
            IDb::dbStartTrans();

            $agent_id = IDb::getInstance("agent")->setDbData(['is_delete'=>IsDelete::No])->add();
            if($agent_id === false){
                //回滚事物
                IDb::dbRollback();

                self::set_code(self::WARNING_0004);
                self::set_msg("注册失败，请联系客服！");
                self::send();
            }

            //添加用户信息
            $user_data['agent_id'] = $agent_id;
            $user_data['phone'] = $user_phone;
            $user_data['nick'] = get_string_rsa($user_phone, 3, 4) . "_" . rand(1000,9999);
            $user_data['agent_admin_status'] = AgentAdminStatus::normal;
            $user_data['admin_super'] = AdminSuper::Super;
            $user_data['is_leader'] = IsLeader::Yes;
            $user_data['reg_ip'] = get_client_ip();
            $user_data['reg_time'] = get_date_time();
            $user_id = IDb::getInstance("agent_admin")->setDbData($user_data)->add();
            if($user_id === false){
                //回滚事物
                IDb::dbRollback();

                self::set_code(self::WARNING_0003);
                self::set_msg("短信登录失败，请联系客服！");
                self::send();
            }

            //清除缓存
            IAgentAdminCache::deleteList();

            //提交事物
            IDb::dbCommit();

            $first_login = IsYes::yes;
        }else{
            $user_id = $user_info['agent_admin_id'];
        }

        //修改最后登录时间
        $user_data = null;
        $user_where = null;
        $user_where['agent_admin_id'] = array("eq",$user_id);
        $user_data['login_ip'] = get_client_ip();
        $user_data['login_time'] = get_date_time();
        $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0004);
            self::set_msg("登录失败！");
            self::send();
        }

        //清除用户缓存
        IAgentAdminCache::deleteInfo($user_id);

        //取得用户信息
        $user_info = IAgentAdminCache::getInfo($user_id);

        //判断用户是否被加入黑名单
        if($user_info['agent_admin_status'] == AgentAdminStatus::blacklist){
            self::set_code(self::ERR_CHECK_0006);
            self::set_msg("亲，您的账号已被禁用，请联系客服！");
            self::send();
        }

        //设置用户信息
        if(!empty($user_info['real'])){ $user_info['real'] = get_string_rsa($user_info['real'],1); }//设置身份证信息
        if(!empty($user_info['identity'])){ $user_info['identity'] = get_string_rsa($user_info['identity'],5,8); }//设置身份证信息

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        //设置数据
        $this->set_token('_u',$user_info['agent_admin_id']); //设置设备编号

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,$this->get_token());
        self::set_value('first_login',$first_login);
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：用户退出
     * @开发者：陈旭林
     */
    public function logout(){
        //接收参数
        $device  = $this->get_data('device_no',self::NOT_EMPTY,"设备编号错误！");
        $os = $this->get_data('os',self::NOT_EMPTY,"系统区分错误！");
        $version  = $this->get_data('version',self::NOT_EMPTY,"系统版本错误！");

        //检测保存设备ID
        if (empty($device) || strlen($device) != 32) {
            self::set_code(self::ERR_0001);
            self::set_msg("设备编号错误！");
            self::send();
        }

        //判断系统区分是否正确
        if(!in_array($os,array(Source::Android,Source::Ios))){
            self::set_code(self::ERR_0002);
            self::set_msg("系统区分错误！");
            self::send();
        }

        //设置数据
        $this->set_token(self::USER,null); //设置用户编号
        $this->set_token(self::DEVICE,$device); //设置设备编号
        $this->set_token(self::OS,$os); //设置系统区分
        $this->set_token(self::VERSION,$version); //设置版本信息

        //返回参数
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,$this->get_token());
        self::send();
    }

    /**
     * @功能：取得登录用户信息
     * @开发者：cxl
     */
    public function login_info(){
        //接收参数
        $user_info = self::$info;

        $user_info['is_phone'] = empty($user_info['phone']) ? IsPhone::not : IsPhone::yes;

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        $agent_info = IAgentCache::getInfo($user_info['agent_id']);
        if(!$agent_info){
            $agent_info = [];
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($user_info);
        self::set_value('agent_info',$agent_info);
        self::send();
    }

    /**
     * @功能：设置用户信息
     * @开发者：gys
     */
    public function set_info(){
        $user_id = self::$uid;
        $user_data['avatar']  = self::get_data("avatar",self::NOT_EMPTY,"头像不能为空！");
        $user_data['nick']  = self::get_data("nick",self::NOT_EMPTY,"昵称不能为空！");
        $user_data['business_phone']  = self::get_data("business_phone",self::NOT_EMPTY,"业务手机不能为空！");
        $user_data['service_ote']  = self::get_data("service_ote");

        //设置用户昵称
        $user_where['agent_admin_id'] = $user_id;
        $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0003);
            self::set_msg("修改用户信息失败！");
            self::send();
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($user_id);
        IAgentAdminCache::deleteList();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置用户昵称
     * @开发者：cxl
     */
    public function set_field(){
        //接收参数
        $user_id = self::$uid;
        $field  = self::get_data("field",self::NOT_EMPTY,"请输入修改字段！");
        $value  = self::get_data("value");

        //判断是否输入
        if(empty($value)){
            self::set_code(self::WARNING_0001);
            self::set_msg("请输入用户信息！");
            self::send();
        }

        //判断字段是否正确
        $field = strtolower($field);
        $field_array = array("nick","avatar");
        if(!in_array($field,$field_array)){
            self::set_code(self::WARNING_0002);
            self::set_msg("修改用户信息错误，请联系客服！");
            self::send();
        }

        //设置用户昵称
        $user_where['agent_admin_id'] = $user_id;
        $user_data[$field] = $value;
        $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0003);
            self::set_msg("修改用户昵称失败！");
            self::send();
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($user_id);
        IAgentAdminCache::deleteList();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：实名验证接口
     * @开发者：cxl
     */
    public function set_real(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $real = self::get_data('real');
        if(!empty($real)){
            $user_data['real'] = $real;
            $user_data['id_card_head'] = self::get_data('id_card_head',self::NOT_EMPTY,"请上传身份证头像那面！");
            $user_data['id_card_national'] = self::get_data('id_card_national',self::NOT_EMPTY,"请上传身份证国徽那面！");
            $user_data['identity'] = self::get_data('identity',self::NOT_EMPTY,"请输入用户身份证号！");
            $user_data['audit_state'] = AuditStatus::Wait;
            //设置身份证信息
//            $real_temp = get_string_rsa($user_data['real'],0,1);
//            $identity_temp = get_string_rsa($user_data['identity'],5,8);

            //判断用户是否已经实名
//            if($user_info['is_real'] == IsReal::yes){
                //设置身份证信息
//                if(!empty($user_info['real'])){
//                    $real_temp = get_string_rsa($user_info['real'],1);
//                }
//
//                //设置身份证信息
//                if(!empty($user_info['identity'])){
//                    $identity_temp = get_string_rsa($user_info['identity'],5,8);
//                }

                //操作成功
//                self::set_code(self::WARNING_0002);
//                self::set_msg("用户已经实名验证了，不能修改实名信息！");
//                self::set_value("real",$real_temp);
//                self::set_value("identity",$identity_temp);
//                self::send();
//            }
        }

        $agent_name = self::get_data('agent_name');
        if(!empty($agent_name)){
            $agent_data['agent_name'] = $agent_name;
            $agent_data['province_id'] = self::get_data('province_id',self::NOT_EMPTY,"请选择企业所在省份！");
            $agent_data['city_id'] = self::get_data('city_id',self::NOT_EMPTY,"请选择企业所在城市！");
            $agent_data['area_id'] = self::get_data('area_id',self::NOT_EMPTY,"请选择企业所在区域！");
            $agent_data['address'] = self::get_data('address',self::NOT_EMPTY,"请输入企业详细地址！");
            $agent_data['business_license_url'] = self::get_data('business_license_url',self::NOT_EMPTY,"请上传企业营业执照！");
            $agent_data['is_authentication'] = IsAuthentication::No;

        }

        IDb::dbStartTrans();

        $agent_info = IAgentCache::getInfo($user_info['agent_id']);
        if(!empty($agent_data) && $agent_info['is_authentication'] != IsAuthentication::Yes){
            $result = IDb::getInstance("agent")
                ->setDbWhere(['agent_id'=>$user_info['agent_id']])
                ->setDbData($agent_data)
                ->upd();
            if($result === false){
                IDb::dbRollback();
                self::set_code(self::WARNING_0004);
                self::set_msg("修改企业信息失败！");
                self::send();
            }
        }

        if(!empty($user_data) && $user_info['audit_state'] != AuditStatus::OK){
            //判断实名是否通过
//            $real_info = IJuHe::real($user_data['real'],$user_data['identity']);
//            if($real_info === false){
//                self::set_code(self::WARNING_0003);
//                self::set_msg("用户实名失败！");
//                self::send();
//            }

            //取得身份证自带信息
            $identity_info = IIdentity::getInfo($user_data['identity']);
            if(!empty($identity_info)){
                $user_data['sex'] = $identity_info['sex'];
                $user_data['age'] = $identity_info['age'];
                $user_data['birthday'] = $identity_info['date'];
            }

            //修改用户实名信息
            $user_where = null;
            $user_where['agent_admin_id'] = array("eq",$user_id);
            $user_data['nick'] = $user_data['real'];
//            $user_data['is_real'] = IsReal::yes;
//            $user_data['real_time'] = get_date_time();
            $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
            if($user_upd === false){
                IDb::dbRollback();
                self::set_code(self::WARNING_0004);
                self::set_msg("修改用户信息失败！");
                self::send();
            }

            //删除缓存
            IAgentAdminCache::deleteInfo($user_id);
        }

        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
//        self::set_value("real",$real_temp);
//        self::set_value("identity",$identity_temp);
        self::send();
    }

    /**
     * @功能：修改密码
     * @开发者：cxl
     */
    public function upd_pwd(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $user_verification = self::get_data('user_verification',self::NOT_EMPTY,"请输入短信验证码！");
        $new_pwd = self::get_data('new_pwd',self::NOT_EMPTY,"请输入新密码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::XD,$user_info['phone']);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的短信验证码错误！");
            self::send();
        }

        //整理用户信息
        $user_where['agent_admin_id'] = $user_id;
        $user_data['pwd'] = md5(md5($new_pwd));
        $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("修改用户密码失败，请联系客服！");
            self::send();
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：找回密码
     * @开发者：cxl
     */
    public function find_pwd(){
        //接收参数
        $user_phone = self::get_data('user_phone',self::NOT_EMPTY,"请输入电话号码！");
        $verification = self::get_data('verification',self::NOT_EMPTY,"请输入验证码！");
        $new_pwd = self::get_data('new_pwd',self::NOT_EMPTY,"请输入新密码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::XD,$user_phone);
        if (!in_array($verification,$verify_list)) {
            $this->set_code(self::WARNING_0001);
            $this->set_msg("亲，您输入的短信验证码错误！");
            $this->send();
        }

        //判断手机号是否已经存在
        $user_where = null;
        $user_where['phone'] = array("eq",$user_phone);
        $user_info = IDb::getInstance("agent_admin")->setDbWhere($user_where)->row();
        if(empty($user_info)){
            self::set_code(self::WARNING_0002);
            self::set_msg("该手机号尚未注册！");
            self::send();
        }

        //整理用户信息
        $user_where = null;
        $user_data = null;
        $user_where['agent_admin_id'] = $user_info['agent_admin_id'];
        $user_data['pwd'] = md5(md5($new_pwd));
        $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("修改用户密码失败，请联系客服！");
            self::send();
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($user_info['agent_admin_id']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置支付密码
     * @开发者：cxl
     */
    public function set_pay_pwd(){
        //接收参数
        $user_id = self::$uid;
        $pay_pwd  = self::get_data("pay_pwd",self::NOT_EMPTY,"请输入支付密码！");

        //取得用户账户信息
        $user_where['admin_agent_id'] = $user_id;
        $user_data['pay_pwd'] = md5(md5($pay_pwd));
        $user_data['is_pay_pwd'] = IsPayPwd::Yes;
        $user_upd = IDb::getInstance("admin_agent")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("设置支付密码失败！");
            self::send();
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置支付密码
     * @开发者：cxl
     */
    public function upd_pay_pwd(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $old_pay_pwd  = self::get_data("old_pay_pwd",self::NOT_EMPTY,"请输入旧支付密码！");
        $new_pay_pwd  = self::get_data("new_pay_pwd",self::NOT_EMPTY,"请输入新支付密码！");

        //判断用户是否设置支付密码
        if($user_info['pay_pwd'] != md5(md5($old_pay_pwd))){
            self::set_code(self::WARNING_0001);
            self::set_msg("您输入的旧支付密码错误！");
            self::send();
        }

        //取得用户账户信息
        $user_where['agent_admin_id'] = $user_id;
        $user_data['pay_pwd'] = md5(md5($new_pay_pwd));
        $user_data['is_pay_pwd'] = IsPayPwd::Yes;
        $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("修改支付密码失败！");
            self::send();
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：找回支付密码-修改密码
     * @开发者：陈旭林
     */
    public function find_pay_pwd(){
        //接收参数
        $user_id = self::$uid;
        $user_verify = self::get_data('user_verify', self::NOT_EMPTY,"请输入验证码！");
        $pay_pwd  = self::get_data('pay_pwd',self::NOT_EMPTY,"请输入新密码！");

        //取得用户手机号
        $user_phone = self::$info['phone'];

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::XD,$user_phone);
        if (!in_array($user_verify,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的短信验证码错误！");
            self::send();
        }

        //取得用户账户信息
        $user_where['agent_admin_id'] = $user_id;
        $user_data['pay_pwd'] = md5(md5($pay_pwd));
        $user_data['is_pay_pwd'] = IsPayPwd::Yes;
        $user_upd = IDb::getInstance("agent_admin")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("找回支付密码失败！");
            self::send();
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：订单列表
     * @开发者：gys
     */
    public function bid_list(){
        $tender_status = self::get_data('tender_status');
        $bottom_id = self::get_data('bottom_id');

        if(!empty($tender_status)) $where['aat.tender_status'] = $tender_status;
        if(!empty($bottom_id)) $where['concat(1,lpad(agent_admin_tender_id,8,0))'] = array("lt",$bottom_id);

        $where['aat.agent_admin_id'] = self::$uid;
        $list = IDb::getInstance('user_pool_bid as upb')
            ->setDbFiled('aat.agent_admin_tender_id,aat.tender_status,aat.tender_status,aat.open_time,
                upb.user_pool_bid_id,upb.bid_no,upb.user_id,upb.city_id,upb.money,upb.period,upb.bid_num,upb.pool_bid_status,upb.bid_start,upb.bid_end,upb.agent_admin_id')
            ->setDbJoin("agent_admin_tender as aat","upb.user_pool_bid_id = aat.user_pool_bid_id")
            ->setDbWhere($where)
            ->setDbOrder("aat.agent_admin_tender_id desc")
            ->setDbLimit(10)
            ->sel();

        //整理数据
        if(!empty($list) && is_array($list) && count($list) > 0){
            $expire_hour = get_config('OpenBidDate',0);

            //循环整理数据
            foreach($list as &$data){
                $user_info = IUserCache::getInfo($data['user_id'],['real','sex','occupation','is_car','is_house','is_credit_card','is_accumulation_fund','is_policy','is_social','is_company']);

                $data['real'] = $user_info['real'];
                $data['city'] = IAreaCache::getName($data['city_id']);
                $data['bid_start_timestamp'] = strtotime($data['bid_start']);
//                $data['bid_end_timestamp'] = strtotime($data['bid_end']);
                $data['bid_end_timestamp'] = strtotime($data['bid_end']." +{$expire_hour} hour");
//                $data['bid_expire_timestamp'] = strtotime($data['bid_end']." +{$expire_hour} hour");
                $data['occupation'] = get_enum_name('occupation',$user_info['occupation']);

                $data['open_time'] = substr($data['open_time'],0,10);
//                $data['bid_end'] = substr($data['bid_end'],0,10);
                $data['bid_end'] = date('Y-m-d',$data['bid_end_timestamp']);

                $data['not_log_day'] = 0;
                if($data['tender_status'] == TenderStatus::Bid) {
                    $where = [];
                    $where['user_pool_bid_id'] = $data['user_pool_bid_id'];
                    $where['task_type'] = IsTrack::Yes;
                    $log_info = IDb::getInstance("user_pool_bid_task")
                        ->setDbFiled("operate_time")
                        ->setDbWhere($where)
                        ->setDbOrder("user_pool_bid_task_id desc")
                        ->row();
                    $time = strtotime($log_info['operate_time']);

                    if(!empty($log_info) && $time < time()){
                        $diff = intval((time()-$time)/3600/24);
                        if($diff >= 3){
                            $data['not_log_day'] = $diff;
                        }
                    }
                }else if($data['tender_status'] == TenderStatus::BidStop){

                }else{
                    $data['real'] = get_real_cover($user_info['real'],$user_info['sex']);
                }

                $access_list = [];
                if($user_info['is_car'] == IsCar::Yes){
                    $access_list[] = '有车辆';
                }
                if($user_info['is_house'] == IsHouse::Yes){
                    $access_list[] = '有房产';
                }
                if($user_info['is_credit_card'] == IsCreditCard::Yes){
                    $access_list[] = '有信用卡';
                }
                if($user_info['is_accumulation_fund'] == IsAccumulationFund::Yes){
                    $access_list[] = '有公积金';
                }
                if($user_info['is_policy'] == IsPolicy::Yes){
                    $access_list[] = '有保单';
                }
                if($user_info['is_social'] == IsSocial::Yes){
                    $access_list[] = '有社保';
                }
                if($user_info['is_company'] == IsCompany::Yes){
                    $access_list[] = '有公司';
                }

                $data['access_list'] = $access_list;

                unset($data['bid_start']);

                //设置排序编码
                $data['code'] = '1'.str_pad($data['agent_admin_tender_id'],8,0,STR_PAD_LEFT);
            }
        }else{
            $list = [];
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("bid_list",$list);
        self::send();
    }

//    /**
//     * @功能：订单详情
//     * @开发者：gys
//     */
//    public function bid_info(){
//        //接收参数
//        $agent_admin_tender_id  = self::get_data('agent_admin_tender_id',self::NOT_EMPTY,"订单编号不能为空！");
//
//        //判断是否投过标
//        $tender_where['agent_admin_tender_id'] = $agent_admin_tender_id;
//        $tender_info = IDb::getInstance('agent_admin_tender')
//            ->setDbWhere($tender_where)
//            ->row();
//        if(empty($tender_info)){
//            self::set_code(self::WARNING_0001);
//            self::set_msg("订单不存在！");
//            self::send();
//        }
//
//        $where['user_pool_bid_id'] = $tender_info['user_pool_bid_id'];
//        $bid_info = IBid::getInfo($where);
//        if(!$bid_info){
//            self::set_code(self::WARNING_0001);
//            self::set_msg(IBid::getError());
//            self::send();
//        }
//
//        if($tender_info['tender_status'] != TenderStatus::Bid){
//            $bid_info['loan_phone'] = IBid::coverPhone($bid_info['loan_phone']);
//        }
//
//        $bid_info['open_time'] = $tender_info['open_time'];
//        $bid_info['tender_status'] = $tender_info['tender_status'];
//        $data['bid_info'] = $bid_info;
//        $data['report_url'] = IUrl::getCredit($bid_info['user_id']);
//
//        //操作成功
//        self::set_code(self::SUCCESS);
//        self::set_msg("成功！");
//        self::set_values($data);
//        self::send();
//    }

    /**
     * @功能：订单用户详情
     * @开发者：gys
     */
    public function user_asset(){
        //接收参数
        $agent_admin_tender_id  = self::get_data('agent_admin_tender_id',self::NOT_EMPTY,"订单编号不能为空！");

        //判断是否投过标
        $tender_where['agent_admin_tender_id'] = $agent_admin_tender_id;
        $tender_where['agent_admin_id'] = self::$uid;
        $tender_info = IDb::getInstance('agent_admin_tender')
            ->setDbWhere($tender_where)
            ->row();
        if(empty($tender_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("订单不存在！");
            self::send();
        }

        $where['user_pool_bid_id'] = $tender_info['user_pool_bid_id'];
        $bid_info = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->row();
        if(empty($bid_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("订单不存在！");
            self::send();
        }

        $data = \app\common\ext\User::getAsset($bid_info['user_id']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($data);
        self::send();
    }

    /**
     * @功能：添加日常跟踪
     * @开发者：gys
     */
    public function bid_add_log(){
        //接收参数
        $agent_admin_tender_id  = self::get_data('agent_admin_tender_id',self::NOT_EMPTY,"订单编号不能为空！");
        $note  = self::get_data('note',self::NOT_EMPTY,"内容不能为空");
        $video_list  = self::get_data('video_list');
        $audio_list  = self::get_data('audio_list');
        $img_list  = self::get_data('img_list');

        //判断是否投过标
        $tender_where['agent_admin_tender_id'] = $agent_admin_tender_id;
        $tender_where['agent_admin_id'] = self::$uid;
        $tender_info = IDb::getInstance('agent_admin_tender')
            ->setDbWhere($tender_where)
            ->row();
        if(empty($tender_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("订单不存在！");
            self::send();
        }

        if($tender_info['tender_status'] != TenderStatus::Bid){
            self::set_code(self::WARNING_0002);
            self::set_msg("未中标，不可添加跟踪记录！");
            self::send();
        }

        $data['operate_note'] = $note;
        $data['video_list'] = $video_list;
        $data['audio_list'] = $audio_list;
        $data['img_list'] = $img_list;
        $result = ILog::BidTaskLog($tender_info['user_pool_bid_id'],null,null,self::$uid,$data,IsTrack::Yes);
        if(!$result){
            self::set_code(self::WARNING_0001);
            self::set_msg("添加日常跟踪失败！");
            self::send();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：日常跟踪列表
     * @开发者：gys
     */
    public function bid_log_list(){
        //接收参数
        $agent_admin_tender_id  = self::get_data('agent_admin_tender_id',self::NOT_EMPTY,"订单编号不能为空！");
        $bottom_id  = self::get_data('bottom_id');

        //判断是否投过标
        $tender_where['agent_admin_tender_id'] = $agent_admin_tender_id;
        $tender_where['agent_admin_id'] = self::$uid;
        $tender_info = IDb::getInstance('agent_admin_tender')
            ->setDbWhere($tender_where)
            ->row();
        if(empty($tender_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("订单不存在！");
            self::send();
        }

        if(!in_array($tender_info['tender_status'],[TenderStatus::Bid,TenderStatus::BidStop])){
            self::set_code(self::WARNING_0002);
            self::set_msg("未中标，不可查看跟踪记录！");
            self::send();
        }

        //设置查询条件
        $where['user_pool_bid_id'] = $tender_info['user_pool_bid_id'];
        $where['task_type'] = IsTrack::Yes;
        if(!empty($bottom_id)) $where['user_pool_bid_task_id'] = array("lt",$bottom_id);
        $log_list = IDb::getInstance("user_pool_bid_task")
            ->setDbFiled("user_pool_bid_task_id,video_list,audio_list,img_list,operate_note,operate_time")
            ->setDbWhere($where)
            ->setDbOrder("user_pool_bid_task_id desc")
            ->setDbLimit(10)
            ->sel();

        //判断列表是否存在
        if($log_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("订单进度查询日志失败！");
            self::send();
        }

        //解析资源数据
        foreach ($log_list as &$val){
            $val['video_list'] = empty($val['video_list']) ? [] : explode('{|}',$val['video_list']);
            $val['audio_list'] = empty($val['audio_list']) ? [] : explode('{|}',$val['audio_list']);
            $val['img_list'] = empty($val['img_list']) ? [] : explode('{|}',$val['img_list']);
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("log_list",$log_list);
        self::send();
    }

    /**
     * @功能：取得顾问评价
     * @开发者：gys
     */
    public function evaluate_list(){
        //接收参数
        $bottom_id = self::get_data('bottom_id');

        if(!empty($bottom_id)) $where['concat(1,lpad(agent_admin_evaluate_id,8,0))'] = array("lt",$bottom_id);
        $where['aae.agent_admin_id'] = self::$uid;
        $evaluate_list = IDb::getInstance('agent_admin_evaluate aae')
            ->setDbFiled('aae.agent_admin_evaluate_id,aae.content,aae.is_anonymous,aae.create_time,u.avatar,u.real,u.sex')
            ->setDbJoin("user as u","u.user_id=aae.user_id ")
            ->setDbWhere($where)
            ->setDbOrder("agent_admin_evaluate_id desc")
            ->setDbLimit(10)
            ->sel();

        foreach ($evaluate_list as &$val){
            if ($val['is_anonymous'] == IsAnonymous::Yes){
                $val['real'] = get_real_cover($val['real'],$val['sex']);
                $val['avatar'] = '';
            }
            $val['create_time'] = date('Y.m.d H:i',strtotime($val['create_time']));

            //设置排序编码
            $val['code'] = '1'.str_pad($val['agent_admin_evaluate_id'],8,0,STR_PAD_LEFT);
        }

        $agent_admin_info = IAgentAdminCache::getInfo(self::$uid,['evaluate_rate']);
        if(empty($agent_admin_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得顾问信息失败！");
            self::send();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("evaluate_list",$evaluate_list);
        self::set_value("agent_admin_info",$agent_admin_info);
        self::send();
    }
}
